using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.FacManage.Dto;
using ZR.Model.FacManage;
using ZR.Service.FacManage.IFacManageService;
using Infrastructure;

namespace ZR.Service.FacManage
{
    /// <summary>
    /// 工位信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IFacStationService), ServiceLifetime = LifeTime.Transient)]
    public class FacStationService : BaseService<FacStation>, IFacStationService
    {
        /// <summary>
        /// 查询工位信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FacStationDto> GetPageList(FacStationQueryDto parm)
        {
            var predicate = QueryExp(parm);
            var response = new PagedInfo<FacStationDto>();
            var total = 0;
            response.PageSize = parm.PageSize;
            response.PageIndex = parm.PageNum;
            response.Result = Queryable().Where(predicate.ToExpression())
                .LeftJoin<FacLine>((sta, line) => sta.LineId == line.LineId)
                .LeftJoin<FacWorkshop>((sta, line, wo) => line.WorkshopId == wo.WorkshopId)
                .LeftJoin<FacFactory>((sta, line, wo, tory) => wo.FactoryId == tory.FactoryId)
                .LeftJoin<FacLoc>((sta, line, wo, tory,loc) => sta.LocId == loc.LocId)
                .OrderByPropertyNameIF(parm.Sort.IsNotEmpty(), parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc)
                .Select((sta, line, wo, tory, loc) => new FacStationDto
                {
                    StationId = sta.StationId,
                    StationCode = sta.StationCode,
                    StationName = sta.StationName,
                    StationType = sta.StationType,
                    StationSign = sta.StationSign,
                    LocId = sta.LocId,
                    Remark = sta.Remark,
                    CreateBy = sta.CreateBy,
                    CreateTime = sta.CreateTime,
                    UpdateBy = sta.UpdateBy,
                    UpdateTime = sta.UpdateTime,
                    LineId = line.LineId,
                    LineInfo = line.LineCode + "  " + line.LineName,
                    FactoryInfo = tory.FactoryCode + "  " + tory.FactoryName,
                    WorkshopInfo = wo.WorkshopCode + "  " + wo.WorkshopName,
                    LocInfo = loc.LocCode
                }).ToPageList(parm.PageNum, parm.PageSize, ref total);
            response.TotalNum = total;
            return response;
        }

        /// <summary>
        /// 获取工位树信息
        /// </summary>
        /// <param name="disabledFactory">禁用工厂选择</param>
        /// <param name="disabledWorkshop">禁用车间选择</param>
        /// <param name="disabledLine">禁用产线选择</param>
        /// <returns></returns>
        public List<TreeSelectInfo> GetTreeList(bool disabledFactory, bool disabledWorkshop, bool disabledLine)
        {
            List<TreeSelectInfo> trees = new List<TreeSelectInfo>();
            //获取工厂信息
            List<FacFactory> factoryList = this.Context.Queryable<FacFactory>().ToList();
            //获取车间信息
            List<FacWorkshop> workshopList = this.Context.Queryable<FacWorkshop>().ToList();
            //获取产线信息
            List<FacLine> lineList = this.Context.Queryable<FacLine>().ToList();
            //获取工位信息
            List<FacStation> stationList = this.Context.Queryable<FacStation>().ToList();
            foreach (FacFactory fac in factoryList)
            {
                TreeSelectInfo tree = new TreeSelectInfo();
                tree.value = fac.FactoryId;
                tree.label = fac.FactoryCode + " " + fac.FactoryName;
                tree.disabled = disabledFactory;
                foreach (FacWorkshop wo in workshopList.Where(it => it.FactoryId == fac.FactoryId).ToList())
                {
                    TreeSelectInfo woTree = new TreeSelectInfo();
                    woTree.value = wo.WorkshopId;
                    woTree.label = wo.WorkshopCode + " " + wo.WorkshopName;
                    woTree.disabled = disabledWorkshop;
                    foreach (FacLine line in lineList.Where(it => it.WorkshopId == wo.WorkshopId).ToList())
                    {
                        TreeSelectInfo lineTree = new TreeSelectInfo();
                        lineTree.value = line.LineId;
                        lineTree.label = line.LineCode + " " + line.LineName;
                        lineTree.disabled = disabledLine;
                        foreach (FacStation sta in stationList.Where(it => it.LineId == line.LineId).ToList())
                        {
                            lineTree.children.Add(new TreeSelectInfo()
                            {
                                value = sta.StationId,
                                label = sta.StationCode + " " + sta.StationName,
                            });
                        }
                        woTree.children.Add(lineTree);
                    }
                    tree.children.Add(woTree);
                }
                trees.Add(tree);
            }
            return trees;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="StationId"></param>
        /// <returns></returns>
        public FacStation GetInfo(string StationId)
        {
            var response = Queryable()
                .Where(x => x.StationId == StationId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加工位信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public FacStation AddFacStation(FacStation model)
        {
            if (this.Any(it => it.StationCode == model.StationCode))
            {
                throw new CustomException($"系统中已包含{model.StationCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改工位信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFacStation(FacStation model)
        {
            if (this.Any(it => it.StationCode == model.StationCode && it.StationId != model.StationId))
            {
                throw new CustomException($"系统中已包含{model.StationCode}编码信息");
            }
            return Update(model, false);
        }

        /// <summary>
        /// 删除工位信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteFacStation(string[] ids)
        {
            return Delete(ids);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<FacStation> QueryExp(FacStationQueryDto parm)
        {
            var predicate = Expressionable.Create<FacStation>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LineId), it => it.LineId == parm.LineId);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.StationCode), it => it.StationCode == parm.StationCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.StationName), it => it.StationName.Contains(parm.StationName));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.StationType), it => it.StationType == parm.StationType);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.StationSign), it => it.StationSign == parm.StationSign);
            return predicate;
        }
    }
}